import $dayjs from 'dayjs'
// 校验身份证
/**
* @param idCard
*/
export function validateIdCard(idCard) {
  let idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
  let flag = false
  if (idcardReg.test(idCard)) {
    flag = true
  } else {
    flag = false
  }
  return flag
}
/**
* @param idCard
*/
export function getBirthdayFromIdCard(idCard) {
  let birthday = ''
  if (idCard != null && idCard !== '') {
    if (idCard.length === 15) {
      birthday = '19' + idCard.substr(6, 6)
    } else if (idCard.length === 18) {
      birthday = idCard.substr(6, 8)
    }
    birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-')
  }
  return birthday
}
/**
* @param idCard
*/
export function getIdCardAge(IDCard) {
  /**
 * 根据身份证号得到姓别和精确计算年龄
 */
  var sexAndAge = {}
  // 获取用户身份证号码
  var userCard = IDCard
  // 如果身份证号码为undefind则返回空
  if (!userCard) {
    return sexAndAge
  }
  // 获取出生年月日
  var yearBirth = userCard.substring(6, 10)
  var monthBirth = userCard.substring(10, 12)
  var dayBirth = userCard.substring(12, 14)
  // 获取当前年月日并计算年龄
  var myDate = new Date()
  var monthNow = myDate.getMonth() + 1
  var dayNow = myDate.getDay()
  var age = myDate.getFullYear() - yearBirth
  if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
    age--
  }
  // 得到年龄
  sexAndAge.age = age
  // 返回性别和年龄
  return sexAndAge
}
// 通过身份证判断性别
export function getSex(idCard) {
  let sexStr = 0
  if (parseInt(idCard.slice(-2, -1)) % 2 === 1) {
    sexStr = 1
  } else {
    sexStr = 2
  }
  return sexStr
}
// 校验手机号是否正确
export function checkMobile(mobile) {
  let reg = /^1\d{10}$/
  let flag = false
  if (!reg.test(mobile)) {
    flag = false
  } else {
    flag = true
  }
  return flag
}
// 获取星期
export function getWeek(startTime) {
  let text = ''
  let index = $dayjs(startTime).day()
  switch (index) {
    case 1:
      text = '星期一'
      break
    case 2:
      text = '星期二'
      break
    case 3:
      text = '星期三'
      break
    case 4:
      text = '星期四'
      break
    case 5:
      text = '星期五'
      break
    case 6:
      text = '星期六'
      break
    case 0:
      text = '星期日'
      break
    default:
      text = ''
  }
  return text
}
// 过滤空格
export function filterSpace(name) {
  return name.replace(/\s+/g, '')
}
export function checkNum(num) {
  let reg = /^[1-9]\d*$/
  let flag = false
  if (!reg.test(num)) {
    flag = false
  } else {
    flag = true
  }
  return flag
}
// 手机号隐藏带星号
export function getPhoneNum(phone) {
  let str = phone.toString()
  let len = str.length - 7
  let xing = ''
  for (let i = 0; i < len; i++) {
    xing += '*'
  }
  return str.substring(0, 3) + xing + str.substring(str.length - 4)
}
// 姓名带带星号
export function getName(name) {
  let str = name.toString()
  let len = str.length - 1
  let xing = ''
  for (let i = 0; i < len; i++) {
    xing += '*'
  }
  return str.substring(0, 1) + xing
}
export function getNewObj(obj1, obj2) {
  for (let key in obj1) {
    obj1[key] = obj2[key]
  }
  return obj1
}

// 设置跳转评价页面数据
export function setCommentInfo(item, key) {
  let query = {
    doctorDeptName: item.doctorDeptName,
    doctorExperTeamId: item.doctorExperTeamId,
    doctorExperTeamName: item.doctorExperTeamName,
    id: item.id,
    doctorId: item.doctorId,
    doctorName: item.doctorName,
    doctorTitleName: item.doctorTitleName,
    hospitalName: item.hospitalName,
    patientId: item.patientId,
    consultationTypeId: item.consultationTypeId,
    headImg: item.headImg,
    headDoctor: item.headDoctor,
    teamNumber: key ? item[key] : ''
  }
  return encodeURIComponent(JSON.stringify(query))
}
export function getParam(url, name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)")
  var r = url.split('?')[1].match(reg)
  if (r != null) {
    return r[2]
  }
  return ''
}
// 获取未读数
export function getUnreadCount(arr) {
  let result = 0
  if (arr.length) {
    result = arr.reduce((count, { unreadCount }) => count + unreadCount, 0)
  } else {
    result = 0
  }
  return result
}

export function setTotalUnreadCount(arr) {
  setImUnreadCount(getUnreadCount(arr))
}

export function setStrWrap(str) {
  return str.replace('/↵/g', '/n')
}

// 可领取优惠券数据
export function setCouponList(list) {
  return list.map(item => {
    let endTime = wx.dayjs().format('YYYY-MM-DD HH:mm:ss')
    let startTime = wx.dayjs(item.receiveEndTime)
    item.receiveEndTime = wx.dayjs(item.receiveEndTime).format('YYYY-MM-DD')
    item.day = startTime.diff(endTime, 'day')
    item.description = setStrWrap(item.description)
    item.isReceive = false
    return item
  })
}



/** *
 *
 * 获取请求的UUID，指定长度和进制,如
 * getUUID(8, 2)   //"01001010" 8 character (base=2)
 * getUUID(8, 10) // "47473046" 8 character ID (base=10)
 * getUUID(8, 16) // "098F4D35"。 8 character ID (base=16)
 *
 */
export function getUUID(len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  var uuid = []
  var i
  radix = radix || chars.length
  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
  } else {
    var r
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16
        uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  return uuid.join('')
}
